package com.utils;

import java.net.InetSocketAddress;
import java.util.Calendar;

public class SystemUtil {
	public static String getCurMethodName(){
		return Thread.currentThread().getStackTrace()[2].getMethodName();
	}
	public static long getUGKey(int userID, int gameID)
	{
		return ((((long)gameID) << 32) + userID); 
	}
	
	public static long getLongKey(int high, int low)
	{
		return ((((long)high) << 32) + low); 
	}
	
	public static int[] fromLongKey(long key)
	{
		return new int[]{(int)(key >> 32), (int)(key)};
	}
	
	public static void println(Object...objects)
	{
		StringBuilder sb = new StringBuilder();
		Calendar c = Calendar.getInstance();
		sb.append("SysOut [").append(c.getTime()).append("]:");
		for(Object o : objects)
		{
			sb.append(o.toString()).append(" ");
		}
		System.out.println(sb.toString());
	}
	
	public static byte[] ipAddr2bytes(int ip, short port)
	{
		byte[] ipAddrs = new byte[6];
		ipAddrs[0] = (byte) (0xff & (ip >> 24));
		ipAddrs[1] = (byte) (0xff & (ip >> 16));
		ipAddrs[2] = (byte) (0xff & (ip >> 8));
		ipAddrs[3] = (byte) (0xff & ip);
		ipAddrs[4] = (byte) (0xff & port >> 8);
		ipAddrs[5] = (byte) (0xff & port);
		return ipAddrs;
	}
	
	public static int getIPFromipAddr(byte[] ipAddrs)
	{
		Integer[] ip = new Integer[4];
		for(int i = 0; i < 4; i++){
			if(ipAddrs[i] < 0){
				ip[i] = 256 + ipAddrs[i];
			}else{
				ip[i] = (int)ipAddrs[i];
			}
		}
		return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3];
	}
	
	public static short getPortFromipAddr(byte[] ipAddrs)
	{
		return (short) ((ipAddrs[4] << 8) + ipAddrs[5]);
	}
	
	public static String ip2str(int ip)
	{
		int ipb0 = (int) (0x0ff & (ip >> 24));
		int ipb1 = (int) (0x0ff & (ip >> 16));
		int ipb2 = (int) (0x0ff & (ip >> 8));
		int ipb3 = (int) (0x0ff & ip);
		if(ipb1 == 256) ipb1 = 0;
		if(ipb2 == 256) ipb2 = 0;
		
		return ipb0 + "." + ipb1 + "." + ipb2 + "." + ipb3;
	}
	
	public static byte[] ipAddr2bytes(String ip, int port) {
		String ipStr = ip+":"+port;
		byte[] byteIP = ipStr.getBytes();
		return byteIP;
	}
	
	public static String getIp4IPAddress(byte[] byteIP){
		String str = new String(byteIP);
		return str.split(":")[0];
	}
	
	public static String getPort4IPAddress(byte[] byteIP){
		String str = new String(byteIP);
		return str.split(":")[1];
	}
	
	public static String getIPStr4IPAddress(byte[] byteIP){
		return new String(byteIP);
	}
	
	  /**
     * 合并byte数组
     */
    public static byte[] unitByteArray(byte[] byte1,byte[] byte2){
        byte[] unitByte = new byte[byte1.length + byte2.length];
        System.arraycopy(byte1, 0, unitByte, 0, byte1.length);
        System.arraycopy(byte2, 0, unitByte, byte1.length, byte2.length);
        return unitByte;
    }
    
	static public void main(String []args){
		InetSocketAddress remoteAddr = new InetSocketAddress("192.168.1.32", 8000);
		System.out.println(remoteAddr.getAddress().getAddress());
		System.out.println(remoteAddr.getHostName());
		int ip = getIPAddr(remoteAddr);
		System.out.println(remoteAddr);
		System.out.println(ip);
		byte[] bs = SystemUtil.ipAddr2bytes(getIPAddr(remoteAddr), (short) remoteAddr.getPort());
		ip = getIPFromipAddr(bs);
		int port = getPortFromipAddr(bs);
		System.out.println(ip2str(ip));
		System.out.println(getLongKey(30009, 820314922));
		System.out.println("=================");
		byte[] ipAddrs = remoteAddr.getAddress().getAddress();
		ip = getIPAddr(remoteAddr);
		System.out.println("ip="+ip);
		System.out.println(ip2str(ip));
	}
	
	private static int getIPAddr(InetSocketAddress remoteAddr)
	{
		byte[] ipAddrs = remoteAddr.getAddress().getAddress();
		return (((int)ipAddrs[0]) << 24) + (((int)ipAddrs[1]) << 16) + (((int)ipAddrs[2]) << 8)
				+ ipAddrs[3];
	}
	public static long getUGKey(long userID, int gameID)
	{
		return ((((long)gameID) << 40) + userID);
	}
	public static long[] fromLongUGKey(long key)
	{
		return new long[]{((key << 24) >> 24), (int)(key >> 40)};
	}
	
}
